package com.hk.developmentlib.utills;

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.inputmethod.InputMethodManager;

import com.hk.developmentlib.base.DevelopmentKit;
import com.hk.developmentlib.baseben.DeviceInfo;
import com.hk.developmentlib.cache.SimpleDiskCache;
import com.hk.developmentlib.utills.des.Des;

import org.xutils.common.util.LogUtil;

import java.lang.reflect.Method;

/**
 * 常用工具类
 *
 * @author wl
 */
public class DeviceUtils {

    public static String getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);

            return info.versionName;
        } catch (Exception e) {
            return "1.0";
        }
    }

    public static final int getRateHight(int width) {
        float rate = 12.0f / 25.0f;
        int hight = (int) (rate * width);
        LogUtil.d("图片高度：" + hight + "图片比例：" + rate);
        return hight;
    }

    public static final int getRateHight2(int width) {
        int hight = width * 15 / 22;
        return hight;
    }

    public static void changeBooleanPreferences(Context ct, String name, String key, Boolean value) {
        SharedPreferences defaultPreferences = ct.getSharedPreferences(name, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = defaultPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }


    /**
     * 获取设备的屏幕信息
     *
     * @param
     * @return
     */
    public static DeviceInfo getDevicesPix() {
        DisplayMetrics metric = DevelopmentKit.getContext().getResources().getDisplayMetrics();
//        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        DeviceInfo deviceInfo = new DeviceInfo();
        deviceInfo.width = metric.widthPixels; // 屏幕宽度（像素）

        deviceInfo.height = metric.heightPixels; // 屏幕高度（像素）
        deviceInfo.density = metric.density; // 屏幕密度（0.75 / 1.0 / 1.5）
        deviceInfo.densityDpi = metric.densityDpi; // 屏幕密度DPI（120 / 160 / 240）

        return deviceInfo;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }


    public static void hideKeybord(Context mContext) {
        /** 隐藏软键盘 **/
        View view = ((Activity) mContext).getWindow().peekDecorView();
        if (view != null) {
            @SuppressWarnings("static-access")
            InputMethodManager inputmanger = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);

        }
    }

    public static void showKeybord(Context mContext) {
        /** 弹出软键盘 **/
        View view = ((Activity) mContext).getWindow().peekDecorView();
        if (view != null) {
            @SuppressWarnings("static-access")
//            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//            imm.showSoftInput(et_dialog_confirmphoneguardpswd, InputMethodManager.SHOW_IMPLICIT);

                    InputMethodManager inputmanger = (InputMethodManager) mContext.getSystemService(mContext.INPUT_METHOD_SERVICE);
            inputmanger.showSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
        }
    }

    public static boolean checkPermission(String permission) {
        boolean result = false;
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                Class clazz = Class.forName("android.content.Context");
                Method method = clazz.getMethod("checkSelfPermission", String.class);
                int rest = (Integer) method.invoke(DevelopmentKit.getContext(), permission);
                result = (rest == PackageManager.PERMISSION_GRANTED);
            } catch (Exception e) {
                result = false;
            }
        } else {
            PackageManager pm = DevelopmentKit.getContext().getPackageManager();
            if (pm.checkPermission(permission, DevelopmentKit.getContext().getPackageName()) == PackageManager.PERMISSION_GRANTED) {
                result = true;
            }
        }
        return result;
    }

    public static String deviceId;


    public static String getDeviceId() {

        if(TextUtils.isEmpty(deviceId)) {
            try {
                android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager)DevelopmentKit.getContext().getSystemService(Context.TELEPHONY_SERVICE);

                if (checkPermission(Manifest.permission.READ_PHONE_STATE)) {
                    deviceId = tm.getDeviceId();
                }

            } catch (Exception e) {
                LogUtil.e( "获取设备号异常");
            }

            if (TextUtils.isEmpty(deviceId)) {
                deviceId = android.provider.Settings.Secure.getString(DevelopmentKit.getContext().getContentResolver(),
                        android.provider.Settings.Secure.ANDROID_ID);
            }
        }

        return deviceId;
    }


}
